package com.xinnet.mms.resources.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.mms.entity.Resources;
import com.xinnet.mms.resources.dao.ResourcesDao;
import com.xinnet.mms.resources.service.ResourcesService;

@Service("resourcesService")
public class ResourcesServiceImpl implements ResourcesService{
	
	@Autowired
	ResourcesDao resourcesDao;
	
	public List<Resources> getResourcesListByRoleId(String roleId){
		
		List<Resources> resourcesList = resourcesDao.getResourcesListByRoleId(roleId);
		return resourcesList;
	}
	
	public List<Resources> getResourcesListByRoleIdAndParentId(String roleId,String parentId){
		List<Resources> resourcesList = resourcesDao.getResourcesListByRoleIdAndParentId(roleId,parentId);
		return resourcesList;
	}
	
	public List<Object> getMenuTreeListByRoleId(String treeIds,String roleId){
		List<Object> menuTreesList = new ArrayList<Object>();
		List<Resources> resourcesList = resourcesDao.getMenuTreeListByRoleId(roleId);
		String[] treeIdArray = treeIds.split(",");
		for (int i = 0; i < treeIdArray.length; i++) {
			String treeId = treeIdArray[i];
			List<Map<String,Object>> menuTreeList = this.getMenuTreeList(resourcesList,treeId);
			menuTreesList.add(menuTreeList);
		}
		return menuTreesList;
	}
	
	
	private List<Map<String,Object>> getMenuTreeList(List<Resources> resourcesList,String treeId){
		List<Map<String,Object>> menuTreeList = new ArrayList<Map<String,Object>>();
		Map<String,Object> nodeMap = new LinkedHashMap<String, Object>();

		List<Resources> leafResourcesList = new ArrayList<Resources>();
		for (Iterator<Resources> iterator = resourcesList.iterator(); iterator.hasNext();) {
			Resources resources = (Resources) iterator.next();
			String parentId = resources.getParentId()+"";
			String id = resources.getId()+"";
			if(parentId.equals(treeId) && !id.equals(treeId)){
				leafResourcesList.add(resources);
			}
		}
		Collections.sort(leafResourcesList,new Comparator<Resources>() {

			@Override
			public int compare(Resources r1, Resources r2) {
				return new Integer(r1.getId()).compareTo(r2.getId());
			}
		});
		for (Iterator<Resources> iterator = leafResourcesList.iterator(); iterator.hasNext();) {
			Resources resources = (Resources) iterator.next();
			String id = resources.getId()+"";
			String remark = resources.getRemark();
			String url = resources.getUrl();
			
			boolean isParentNode = false;
			for (Iterator<Resources> iterator2 = resourcesList.iterator(); iterator2.hasNext();) {
				Resources resources2 = (Resources) iterator2.next();
				String parentId = resources2.getParentId()+"";
				if(id.equals(parentId)){
					isParentNode = true;
					break;
				}
			}
			if(isParentNode){
				List<Map<String,Object>> leafNodeList = this.getMenuTreeList(resourcesList,id);
				nodeMap.put(remark, leafNodeList);
				
			}else{
				nodeMap.put(remark, url);
			}	
		}
		menuTreeList.add(nodeMap);
		return menuTreeList;	
	}
}
